package com.ruoyi.web.controller.campus;

import java.util.List;
import java.util.UUID;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.campus.domain.MyCommodity;
import com.ruoyi.campus.service.IMyCommodityService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 个人商品信息Controller
 *
 * @author zht
 * @date 2021-12-08
 */
@RestController
@RequestMapping("/campus/userCommodity")
public class MyUserCommodityController extends BaseController
{
    @Autowired
    private IMyCommodityService myCommodityService;

    /**
     * 查询个人商品信息列表
     */
    @PreAuthorize("@ss.hasPermi('campus:userCommodity:list')")
    @GetMapping("/list")
    public TableDataInfo list(MyCommodity myCommodity)
    {
        startPage();
        List<MyCommodity> list = myCommodityService.selectMyCommodityListByUserId(myCommodity,getUserId());
        return getDataTable(list);
    }

    /**
     * 获取个人商品信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('campus:userCommodity:query')")
    @GetMapping(value = "/{comId}")
    public AjaxResult getInfo(@PathVariable("comId") String comId)
    {
        return AjaxResult.success(myCommodityService.selectMyCommodityByComIdAndUserId(comId,getUserId()));
    }

    /**
     * 新增个人商品信息
     */
    @PreAuthorize("@ss.hasPermi('campus:userCommodity:add')")
    @Log(title = "商品信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MyCommodity myCommodity)
    {
        myCommodity.setComId(UUID.randomUUID().toString().replace("-",""));
        myCommodity.setUserId(getUserId());
        myCommodity.setComStatus("3");
        return toAjax(myCommodityService.insertMyCommodity(myCommodity));
    }

    /**
     * 修改个人商品信息
     */
    @PreAuthorize("@ss.hasPermi('campus:userCommodity:edit')")
    @Log(title = "商品信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MyCommodity myCommodity)
    {
        return toAjax(myCommodityService.updateMyCommodity(myCommodity));
    }

    /**
     * 删除个人商品信息
     */
    @PreAuthorize("@ss.hasPermi('campus:userCommodity:remove')")
    @Log(title = "商品信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{comIds}")
    public AjaxResult remove(@PathVariable String[] comIds)
    {
        return toAjax(myCommodityService.deleteMyCommodityByComIds(comIds));
    }

    /**
     * 根据typeId得到商品类别
     */
    @GetMapping(value = "/getType/{typeId}")
    public AjaxResult getType(@PathVariable("typeId") String typeId)
    {
        return AjaxResult.success(myCommodityService.selectCommTypeById(typeId));
    }

    /**
     * 根据typeName得到商品类别
     */
    @GetMapping(value = "/getCommTypeByTypeNameLike/{typeName}")
    public AjaxResult getCommTypeByTypeNameLike(@PathVariable("typeName") String typeName)
    {
        return AjaxResult.success(myCommodityService.getCommTypeByTypeNameLike(typeName));
    }
}
